package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import model.User;

/**
 * This view will be shown to retrieve information about a new user
 * and register them with the program.
 * 
 * @author Tristan D. Boucher
 * @version Commit #22 9/14/2013
 *
 */
@SuppressWarnings("serial")
public class GUIRegistrationPanel extends AbstractView
{
  /**
   * The width of this Panel.
   */
  private static final int WIDTH = 250;

  /**
   * The Height of this panel.
   */
  private static final int HEIGHT = 350;

  /**
   * The text size for this gui.
   */
  private static final int TEXT_SIZE = 16;

  /**
   * The Constant labels for the text boxes.
   * Array Used to factory simple gui.
   */
  private static final String[] MY_LABELS = {"First Name: ",
    "Last Name: ",
    "Student ID #: ",
    "Password: ",
    "Confirm Password: "};

  /**
   * The Constant labels for the buttons.
   * Array Used to factory simple gui.
   */
  private static final String[] MY_BUTTON_LABELS = {"Register",
                                                    "Cancel"};

  /**
   * The constant array of ActionListeners.
   */
  private final ActionListener[] my_action_listeners = {new RegistrationActionListener(),
                                                        new CancelActionListener()};

  /**
   * The array that will hold the real text as the password boxes
   * are *'d in. 
   */
  private JTextField[] my_text_boxes;

  /**
   * Constructor.
   * 
   * @param the_controller The controller for this gui object.
   */
  public GUIRegistrationPanel(final GUIController the_controller)
  {
    super(the_controller, WIDTH, HEIGHT);

    my_text_boxes = new JTextField[MY_LABELS.length];

    addBackground(this, BACKGROUND_COLOR);

    this.add(buildPanel());
  }

  /**
   * Factory makes the gui with a specific look and feel. 
   * 
   * @return A JPanel object that contains a text field for 
   * each required piece of information the user must enter.
   */
  private JPanel buildPanel()
  {
    final JPanel info_panel = new JPanel();
    info_panel.setLayout(new GridLayout(0, 1));

    final JPanel button_panel = new JPanel();
    addBackground(button_panel, BACKGROUND_COLOR);
    
    final JPanel to_return = new JPanel();
    addLinedBorder(addBackground(to_return, BACKGROUND_COLOR), LINED_BORDER_COLOR).
      setLayout(new BorderLayout());
    
    for (int i = 0; i < MY_LABELS.length; i++)
    {
      my_text_boxes[i] = new JTextField(TEXT_SIZE);
      my_text_boxes[i].addKeyListener(new ConfirmKeyListener());
      info_panel.add(addTitledBorder(addBackground(my_text_boxes[i], BACKGROUND_COLOR), 
                                     MY_LABELS[i], TEXT_FIELD_BORDER_COLOR, TEXT_SIZE));
    }

    for (int i = 0; i < MY_BUTTON_LABELS.length; i++)
    {
      final JButton temp_button = new JButton(MY_BUTTON_LABELS[i]);
      addBackground(temp_button, BACKGROUND_COLOR);
      temp_button.addActionListener(my_action_listeners[i]);
      button_panel.add(temp_button);
    }

    to_return.add(new JLabel("User not found. Please Register Here."), 
                  BorderLayout.NORTH);
    to_return.add(info_panel, BorderLayout.CENTER);
    to_return.add(button_panel, BorderLayout.SOUTH);
    
    return to_return;
  }

  /**
   * The action for the registration view.
   */
  private void register()
  {
    final String[] texts = new String[MY_LABELS.length];

    try
    {
      for (int i = 0; i < texts.length; i++)
      {
        texts[i] = my_text_boxes[i].getText().trim();
        if (texts[i].isEmpty())
        {
          JOptionPane.showMessageDialog(GUIRegistrationPanel.this,
                                        "You missed something! Fill in the blank field.",
                                        "Error in Registration",
                                        JOptionPane.ERROR_MESSAGE);
          return;
        }
      }
      
      Integer.parseInt(texts[2]);
      
      if (texts[3].equals(texts[4]))
      {
        my_controller.addUser(new User(texts[0], 
                                       texts[1], 
                                       Integer.parseInt(texts[2]) + "", 
                                       texts[3]));
      }
      else
      {
        JOptionPane.showMessageDialog(GUIRegistrationPanel.this,
                                      "The passwords don't match. Please retype passwords.",
                                      "Error in Registration",
                                      JOptionPane.ERROR_MESSAGE);
      }
    }
    catch (final NumberFormatException exception)
    {
      JOptionPane.showMessageDialog(GUIRegistrationPanel.this,
                                    "Student ID # must contain only numbers.",
                                    "Error entering SID!",
                                    JOptionPane.ERROR_MESSAGE);
    }
  }
  
  /**
   * Listener Class for the Registration Button.
   * 
   * @author Tristan D. Boucher
   * @version Commit #20 9/13/2013
   *
   */
  private class RegistrationActionListener implements ActionListener
  {
    /**
     * Required method from super class.
     * 
     * @param the_e The event object that was thrown.
     */
    public void actionPerformed(final ActionEvent the_e)
    {
      register();
    }

  }

/**
 * Listener class for the Cancel button.
 * 
 * @author Tristan D. Boucher
 * @version Commit #20 9/13/2013
 *
 */
  private class CancelActionListener implements ActionListener
  {
    /**
     * Required method from super class.
     * 
     * @param the_e The event object that was thrown.
     */
    public void actionPerformed(final ActionEvent the_e)
    {
      my_controller.postLogout();
    }
  }
  
  /**
   * Lots of work to get the enter button to register and
   * login.
   * 
   * @author Tristan D. Boucher
   * @version Commit #8 8/27/2013
   */
  private class ConfirmKeyListener implements KeyListener
  {  
    /**
     * Stupid required methods from interface.
     * 
     * @param the_e The event caused.
     */
    public void keyPressed(final KeyEvent the_e)
    {
      //Stupid required methods from interface.
    }

    /**
     * This overridden method posts the login for
     * an enter key released.
     * 
     * @param the_e The event caused.
     */
    public void keyReleased(final KeyEvent the_e)
    {
      if (the_e.getKeyCode() == KeyEvent.VK_ENTER)
      {
        register(); 
      }
    }

    /**
     * Stupid required methods from interface.
     * 
     * @param the_e The event caused.
     */
    public void keyTyped(final KeyEvent the_e)
    {
      //Stupid required methods from interface.
    }
  }
}
